package dbutils;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/2 17:22
 * @since JDK 1.8
 *
 * 将本地图片获取输入流
 */
public class DBImageUtils {


    public static void main(String[] args) {
        readImage2DB(new File("C:\\Users\\xiong\\Pictures\\djw\\src=http___picture.youth.cn_qtdb_201710_W020171026282234980022.jpg&refer=http___picture.youth.jpeg"));
    }

    // 将图片插入数据库
    public static void readImage2DB(File file) {
        Connection conn =new DBSql(DataScore.MY_SQL()).getConnection() ;
        PreparedStatement ps = null;
        FileInputStream in = null;
        try {
            in = readImage(file.getPath());
            String sql = "insert into image (image_name,image_size,type,file)values(?,?,?,?) ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,file.getName());
            ps.setDouble(2,10);
            ps.setString(3,"image");
            ps.setBinaryStream(4, in, in.available());
            int count = ps.executeUpdate();
            System.out.println(count);
            if (count > 0) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    // 读取数据库中图片
    public static void readDB2Image() {
        String targetPath = "C:/Users/xiong/Desktop/123456.jpeg";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = new DBSql(DataScore.MY_SQL_LINUX()).getConnection() ;
            String sql = "select * from image limit 1";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                InputStream in = rs.getBinaryStream("file");
                readBin2Image(in, targetPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    // 读取本地图片获取输入流
    public static FileInputStream readImage(String path) throws IOException {
        return new FileInputStream(new File(path));
    }

    // 读取表中图片获取输出流
    public static void readBin2Image(InputStream in, String targetPath) {
        File file = new File(targetPath);
        String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
        if (!file.exists()) {
            new File(path).mkdir();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
